package cn.findviewbydream.handle;

import cn.findviewbydream.dto.Result;
import cn.findviewbydream.exception.BaseException;
import cn.findviewbydream.utils.ResultUtil;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**********************************
 * 工程：cn.findviewbydream.handle
 * 作者：白雨濃 evil
 * 时间：17-4-17 下午3:50
 * 邮箱：evilbai@foxmail.com
 * 内容：捕获异常
 **********************************/
@ControllerAdvice
public class ExceptionHandle
{
    private static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e)
    {
        if (e instanceof BaseException)
            return ResultUtil.error(((BaseException) e).getCode(), e.getMessage());
        else if (e instanceof SchedulerException)
            return ResultUtil.error(-900, "已经在队列");
        else
        {
            logger.error("未知错误:{}", e.toString());
            e.printStackTrace();
            return ResultUtil.error(-999, "未知错误");
        }
    }
}
